package com.four.project.controller;

import com.four.project.pojo.Manager;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.hibernate.engine.query.spi.ReturnMetadata;
import org.springframework.web.bind.annotation.*;

/**
 * @Description
 * @Author 梯子君(xie)
 * @Data 2020/7/911:52
 */
@RestController
@CrossOrigin
public class ManagerController {

    @RequestMapping(value = "/login",method = RequestMethod.POST)
    public String login(@RequestBody Manager manager){
        if (manager==null||manager.getUsername().equals("")||manager.getPassword().equals("")){
            return "fail";//用户名密码未输入
        }
        UsernamePasswordToken token = new UsernamePasswordToken(manager.getUsername(), manager.getPassword());
        //获取shiro主体
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            //再加一个判断判断是否验证
            if (subject.isAuthenticated()){
                return "success";
            }else {
                return "fail";
            }
        }catch (Exception e){
                return "fail";
        }

    }

    @RequestMapping("/unatch")
    public String unatch(){
        return "权限不足";
    }
    @RequestMapping("/unlogin")
    public String unlogin(){
        return "您未登录,请您前往登录";
    }
}
